package com.crm.services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.model.jdao.IOrderInfoDao;
import com.crm.model.jdao.IRefundgoodDAO;
import com.crm.model.mdao.OrderInfoMapper;
import com.crm.model.mdao.ProductMapper;
import com.crm.model.mdao.RefundgoodMapper;
import com.crm.model.mdao.RefunditemsMapper;
import com.crm.model.mpojo.Product;
import com.crm.model.mpojo.ProductWithBLOBs;
import com.crm.model.mpojo.Refunditems;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Refundgood;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

@Service
@Transactional(rollbackFor = Exception.class)
public class RefundgoodService {
	
	@Autowired
	IOrderInfoDao iOrderInfoDao;
	
	@Autowired
	IRefundgoodDAO iRefundgoodDAO;
	
	@Autowired
	OrderInfoMapper orderInfoMapper;
	
	@Autowired
	RefundgoodMapper refundgoodMapper;
	
	@Autowired
	RefunditemsMapper refunditemsMapper;
	
	@Autowired
	ProductMapper productMapper;
	@Autowired
	TimeLineServices timeLineServices;
	
	/**
	 * 上一年度盈利统计
	 * @return 
	 */
	public Map<String, String> yearProfile() {
		return refundgoodMapper.yearProfile();
	}
	
	/**
	 * 分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,Integer statusid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer dstatus=null;
		
		if(statusid!=null) {
		if(statusid==1||statusid==2)
			dstatus=statusid;
		}
		System.out.println("查询值"+dstatus);
		// 获取
		List<Map<String, String>> list = refundgoodMapper.selectAll(statusid,search);
		System.out.println(list);
		System.out.println("大小" + list.size());
		return list ;
	}
	
	
	
	/**
	 * 添加退货信息
	 * @param limap
	 * @param id
	 * @param sumMoney
	 * @return 
	 */
	public Integer addreturnGoodsview(List<Map<String, String>> limap, Integer id,String sumMoney) {
		//退货表信息
		Refundgood refundgood = new Refundgood();
		Orderinfo orderinfo = iOrderInfoDao.findById(id).get();
		com.crm.model.mpojo.Orderinfo selectByPrimaryKey = orderInfoMapper.selectByPrimaryKey(id);
		refundgood.setOrderinfo(orderinfo);
		refundgood.setRgTitle(selectByPrimaryKey.getCustId()+"");
		refundgood.setRgMoney(new BigDecimal(sumMoney));
		refundgood.setRgDate(new Date());
		refundgood.setRgStatus(1253);
		refundgood.setRemark(LoginUtil.getUser().getuId()+"");
		refundgood.setSrefundstatus(1);
		Refundgood save = iRefundgoodDAO.save(refundgood);
		//退货详情
		/**
		 * {"odDiscount":1,"riResult":"","pName":"小米","pUnit":"台","pId":5,"riPrice":2000,"riMoney":2000,"riCount":1}
		 */
		for (Map<String, String> map : limap) {
			System.out.println(map);
			Refunditems refunditems = new Refunditems();
			refunditems.setpId(Integer.parseInt(String.valueOf(map.get("pId"))));
			refunditems.setRgId(save.getRgId());
			refunditems.setRiCount(Integer.parseInt(String.valueOf(map.get("riCount"))));
			refunditems.setRiMoney(new BigDecimal(String.valueOf(map.get("riMoney"))) );
			refunditems.setRiPrice(new BigDecimal(String.valueOf(map.get("riPrice"))));
			refunditems.setRiResult(String.valueOf(map.get("riResult")));
			refunditemsMapper.insertSelective(refunditems);
			ProductWithBLOBs selectByPrimaryKey2 = productMapper.selectByPrimaryKey(Integer.parseInt(String.valueOf(map.get("pId"))));
			Timeline timeline=new Timeline();
			timeline.setTheme("订单退货");
			timeline.setTimes(new Date());
			timeline.setCustId(orderinfo.getCustomer().getCustId());
			timeline.setDetails("退货商品:"+selectByPrimaryKey2.getpName()+"   "+Integer.parseInt(String.valueOf(map.get("riCount")))+"件         金额：￥"+String.valueOf(map.get("riMoney")));
			timeLineServices.insertTimeLine(timeline);


			
		}
		
		return save.getRgId();
		
		
		
	}
	/**
	 * 修改退货单状态
	 * @param refundgood
	 */
	public void updateType(com.crm.model.mpojo.Refundgood refundgood,Integer oId) {
		com.crm.model.mpojo.Orderinfo orderinfo=new com.crm.model.mpojo.Orderinfo();
		refundgood.setSrefundstatus(2);
		orderinfo.setoId(oId);
		orderinfo.setoSalesreturn(1095);
		orderInfoMapper.updateByPrimaryKeySelective(orderinfo);
		refundgoodMapper.updateByPrimaryKeySelective(refundgood);
	}
	
	
	
	

}
